package com.anli.mideng;

import jdk.nashorn.internal.ir.JoinPredecessor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/10  10:53
 * 描述:
 */
@Aspect
@Component
public class MeDengAop {

    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("execution(public * com.anli.web.*.*(..))")
    public void pointcut(){}


    @Around("pointcut() && @annotation(com.anli.mideng.MeDeng)")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取请求中的token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String tokenMx = request.getHeader("tokenMx");

        Boolean delete = redisTemplate.delete(tokenMx);
        if(delete){
            return joinPoint.proceed();
        }else{
            HttpServletResponse response = requestAttributes.getResponse();
            response.encodeURL("utf-8");
            response.getWriter().append("success");
            return null;
        }

    }

}
